const app = getApp()
const login = require('../../utils/login.js')
Page({
  data: {
    hasUserInfo: false,
    canIUse: false,
    cardType:0,
    accountInfo:{},
    recharge1:'20,000',
    recharge2:'100,000',
    swiperPage:0
  },
  onLoad: function (options) {
    this.getAccountInfo()
    login.getIfLogin(this)
  },
  onShow(){
    // this.getAccountInfo()
  },
  onTabItemTap(){
    this.getAccountInfo()
    login.getIfLogin(this)
  },
  bindCharge(){
    wx.navigateTo({
      url:'../recharge/recharge'
    })
  },
  bindCardSwiper(e){
    this.setData({
      cardType:e.detail.currentItemId
    })
  },
  getAccountInfo(){
    app.request('ugc/getAccountInfo').then(res=>{
      console.log(res)
      wx.hideLoading()
      if(res.success){
        if(res.content.gradeId==0){
          let num = Number(res.content.totalRecharge),
          num2 = Number(res.content.totalRecharge)
          // recharge1 = this.toThousands(num),
          // recharge2 = this.toThousands(num2)
          let recharge1 = num,
              recharge2 = num2
          // if(num>1){
          //   recharge1 = this.toThousands(num)
          // }
          // if(num2>1){
          //   recharge2 = this.toThousands(num2)
          // }
          this.setData({
            recharge1,
            recharge2,
            cardType:res.content.gradeId
          })
        }else if(res.content.gradeId==1){
          let num = Number(res.content.totalRecharge),
          recharge2 = num
          // recharge2 = this.toThousands(num)
          this.setData({
            recharge2,
            cardType:res.content.gradeId
          })
        }else{
          this.setData({
            cardType:res.content.gradeId
          })
        }
        // if(res.content.balance>1){
        //   res.content.balance = this.toThousands(res.content.balance)
        // }
        this.setData({
          accountInfo:res.content
        })
      }else{
        console.log(res.message)
      }
    })
  },
  toThousands(num) {
    var result = [ ], counter = 0;
    num = (num || 0).toString().split('');
    for (var i = num.length - 1; i >= 0; i--) {
        counter++;
        result.unshift(num[i]);
        if (!(counter % 3) && i != 0) { result.unshift(','); }
    }
    return result.join('');
  },
  getUserInfo: function (e) {
    var that = this
    console.log(e)
    if (e.detail.userInfo) {
      wx.setStorageSync("userInfo", e.detail.userInfo);
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      console.log("点击了同意授权");

      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          if (res.code) {
            console.log(res.code)
            wx.setStorageSync('code', res.code)
            wx.getUserInfo({
              success: ress => {
                console.log(ress)
                let data = {
                  "code": res.code,
                  "headImage": that.data.userInfo.avatarUrl,
                  "nickName": that.data.userInfo.nickName,
                  "userInfo": JSON.stringify(that.data.userInfo),
                  "encryptedData": ress.encryptedData,
                  "iv": ress.iv,
                  "appType":"yqs"
                }
                console.log(data)
                app.request('oauth/token', data, 'POST').then(item => {
                  console.log(item)
                  if (item.success) {
                    
                    wx.hideLoading()
                    wx.setStorageSync('token', item.content)
                    that.setData({
                      canIUse: true
                    })
                    that.getAccountInfo()
                  } else {
                    wx.hideLoading()
                    that.setData({
                      hasUserInfo: false
                    })
                  }
                })
              }
            })
          } else {
            console.log('获取用户登录失败！' + res.errMsg)
            that.setData({
              hasUserInfo: false
            })
          }
        },
        fail: res => {
          that.setData({
            hasUserInfo: false
          })
        }
      })

    }
  },
  bindDetail(e){
    wx.navigateTo({
      url:'../detail/detail?type='+e.currentTarget.dataset.id+'&page=1&cardType='+this.data.cardType
    })
  },
})